//
//  ViewController.m
//  TestPickerView
//
//  Created by weihong xuan on 2017/12/12.
//  Copyright © 2017年 weihong xuan. All rights reserved.
//

#import "ViewController.h"
#define SCREEN_WIDTH                                  ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT                                 ([UIScreen mainScreen].bounds.size.height)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define Color_Background                     UIColorFromRGB(0xeeeeee)
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color_Background;
    
    _SendAddressDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self setUpView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}
- (void)setUpView{
    [UIColor blackColor];
    UIView*tfBg = [UIKitFactory createViewWithFrame:CGRectMake(0, 164, SCREEN_WIDTH, 50) backgroundColor:[UIColor whiteColor] superView:self.view];
    _ChooseAdrBtn = [UIKitFactory createButtonWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:@"请选择省、市、区" setImage:@"sale_more" setBgImg:nil font:[UIFont systemFontOfSize:15] titleColor:[UIColor blackColor] backgroundColor:[UIColor whiteColor] SuperView:tfBg];
    [_ChooseAdrBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 40)];
    [_ChooseAdrBtn setImageEdgeInsets:UIEdgeInsetsMake(18.5, SCREEN_WIDTH-30, 18.5, 23)];
    _ChooseAdrBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_ChooseAdrBtn addTarget:self action:@selector(clickChooseAdrBtn) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)clickChooseAdrBtn{
    [self.view endEditing:YES];
    _pickerView = [[MXPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _pickerView.dataType = Area;
    [self loadAreaWithHierarchy];
    [_pickerView showAnimation];
    __weak __typeof (self)  weakSelf = self;
    _pickerView.addressBlock = ^(NSString *province,NSString *city,NSString *town) {
        weakSelf.provinceStr = province;
        weakSelf.cityStr     = city;
        weakSelf.areaStr     = town;
        [weakSelf.ChooseAdrBtn setTitle:[NSString stringWithFormat:@"%@,%@,%@",province,city,town] forState:UIControlStateNormal];
    };
}
- (void)loadAreaWithHierarchy{
    [_pickerView.arrProvince removeAllObjects];
    [_pickerView.arrCity removeAllObjects];
    [_pickerView.arrArea removeAllObjects];
    NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"]];
    [_pickerView.arrProvince addObjectsFromArray:@[@"北京市",@"上海市",@"天津市",@"重庆市",@"河北省",@"山西省",@"内蒙古自治区",@"辽宁省",@"吉林省",@"黑龙江省",@"江苏省",@"浙江省",@"安徽省",@"福建省",@"广东省",@"广西壮族自治区",@"湖南省",@"湖北省",@"山东省",@"陕西省",@"河南省",@"甘肃省",@"四川省",@"青海省",@"江西省",@"云南省",@"贵州省",@"宁夏回族自治区",@"西藏自治区",@"新疆维吾尔自治区",@"海南省"]];
    NSString*keyStr = _pickerView.arrProvince[0];
    NSDictionary*tmpDicCity = [dataDict objectForKey:keyStr][0];
    NSArray*tmpAryCity = [tmpDicCity allKeys];
    [_pickerView.arrCity addObjectsFromArray:tmpAryCity];
    NSString*keyAreaStr = _pickerView.arrCity[0];
    NSArray*tmpAryArea = [tmpDicCity objectForKey:keyAreaStr];
    [_pickerView.arrArea addObjectsFromArray:tmpAryArea];
    _strProvinceKey = _pickerView.arrProvince[0];
    __weak __typeof (self) weakSelf = self;
    _pickerView.reloadBlock = ^(NSInteger index, NSString *areaId, NSInteger row) {
        if (index==0) {
            weakSelf.strProvinceKey = areaId;
            [weakSelf.pickerView.arrCity removeAllObjects];
            [weakSelf.pickerView.arrArea removeAllObjects];
            NSDictionary*tmpDicCity = [dataDict objectForKey:areaId][0];
            NSArray*tmpAryCity = [tmpDicCity allKeys];
            [weakSelf.pickerView.arrCity addObjectsFromArray:tmpAryCity];
            NSString*keyAreaStr = weakSelf.pickerView.arrCity[0];
            NSArray*tmpAryArea = [tmpDicCity objectForKey:keyAreaStr];
            [weakSelf.pickerView.arrArea addObjectsFromArray:tmpAryArea];
        }
        else if (index==1){
            [weakSelf.pickerView.arrArea removeAllObjects];
            NSString*keyStr = weakSelf.strProvinceKey;
            NSArray*tmpAryArea = [dataDict objectForKey:keyStr][0][areaId];
            [weakSelf.pickerView.arrArea addObjectsFromArray:tmpAryArea];
        }
        [weakSelf.pickerView reloadData];
    };
}



@end
